package org.deng.learn.zookeeper.simple;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

/**
 * @Author DengLibin
 * @Date 19-5-12
 * @Time 下午4:52
 * @Desc 案例: 服务器动态上下线
 */
public class Simple {


    public static  void main(String[] args) throws IOException, KeeperException, InterruptedException {

        Server server1 = new Server("/servers/server1","server1");
        Server server2 = new Server("/servers/server2","server2");
        Server server3 = new Server("/servers/server3","server3");


        //服务器启动
        server1.start();
        server2.start();
        server3.start();

        //创建监听器开始监听
        ServerListener listener =  new ServerListener("/servers");
        listener.startListen();

        Thread.sleep(5000);
        server1.stop(); //停止一个服务器
        Thread.sleep(5000);
        server2.stop(); //再停止一个服务器

        Thread.sleep(5000);
        server1.start(); //再开启

        Thread.sleep(3000);
        server2.start(); //再开启

        while (true){
            Thread.sleep(1000 * 1000);
        }
    }
}
